package com.adira.fraud.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Query;

import com.adira.fraud.hibernate.HibernateUtil;
import com.adira.fraud.model.pojo.Branch;
import com.adira.fraud.model.pojo.FraudAccusation;
import com.adira.fraud.model.pojo.FraudCategory;
import com.adira.fraud.model.pojo.FraudMapping;
import com.adira.fraud.model.pojo.Media;
import com.adira.fraud.model.pojo.Position;
import com.adira.fraud.model.pojo.Priority;
import com.adira.fraud.model.pojo.User;
import com.adira.fraud.model.view.InboxView;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FraudAccusationAction extends ActionSupport implements SessionAware{

	private Map<String, Object> sessionAttributes = null;
	private String accusatorName;
	private String suspectName;
	private Position accusatorPosition;
	private Position suspectPosition;
	private Branch accustorBranch;
	private Branch suspectBranch;
	private String accusationDescription;
	private Media accusationMedia;
	private List<Position> positions;
	private List<Media> medias;
	private List<Branch> branches;
	private List<InboxView> inboxViews;
	private List<Priority> priorities;
	private List<FraudCategory> fraudCategories;
	private List<User> users;
	private Date today;
	private Integer selectedAccusatorBranchId;
	private Integer selectedAccusatorMediaId;
	private Integer selectedAccusatorPositionId;
	private Integer selectedSuspectPositionId;
	private Integer selectedSuspectBranchId;
	private Integer selectedFraudId;
	private Integer selectedMappedFraudId;
	private FraudAccusation fraudAccusation;
	private String branchName;
	private Integer selectedUserId;
	private Integer selectedFraudCategoryId;
	private Integer selectedPriorityId;
	private String priority;
	private String fraudCategory;
	private String investigator;
	
	public FraudAccusationAction(){
		inboxViews = new ArrayList<InboxView>();
	}
	@Override
    public void setSession(Map<String, Object> arg0) {
        this.sessionAttributes = arg0;
    }
	
	public String execute() {
        return SUCCESS;
    }
	
	public String LoadNewAccusationAction(){
		org.hibernate.Session hibernateSession= HibernateUtil.getSession();
		org.hibernate.Transaction trx = hibernateSession.beginTransaction();
		org.hibernate.Query query = hibernateSession.createQuery("from "+Branch.class.getName());
		branches = query.list();
		query = hibernateSession.createQuery("from "+Media.class.getName());
		medias = query.list();
		query = hibernateSession.createQuery("from "+Position.class.getName());
		positions = query.list();
	//	accusatorName =(String) ActionContext.getContext().getSession().get("username");
		today = new Date();
		HibernateUtil.closeAll(hibernateSession, trx);
		return SUCCESS;
	}
	
	public String addNewAccusationAction(){
		System.out.println("== data pelapor ==");
		System.out.println(selectedAccusatorBranchId+" "+selectedAccusatorMediaId+" "+selectedAccusatorPositionId);
		System.out.println(accusatorName+" "+ today);
		System.out.println("== data suspect ==");
		System.out.println(selectedSuspectPositionId+" "+selectedSuspectBranchId );
		System.out.println(suspectName + " " + accusationDescription);
		org.hibernate.Session hibernateSession= HibernateUtil.getSession();
		org.hibernate.Transaction trx = hibernateSession.beginTransaction();
		
		FraudAccusation fraudAccusation = new FraudAccusation();
		fraudAccusation.setBranchByAccusatorBranchId((Branch)hibernateSession.load(Branch.class, selectedAccusatorBranchId));
		fraudAccusation.setBranchBySuspectBranchId((Branch)hibernateSession.load(Branch.class, selectedSuspectPositionId));
		fraudAccusation.setCaseDetail(accusationDescription);
		fraudAccusation.setCreatedDate(new Date());
		fraudAccusation.setFraudAccusatorId((Integer)ActionContext.getContext().getSession().get("userid"));
		fraudAccusation.setMedia((Media)hibernateSession.load(Media.class, selectedAccusatorMediaId));
		fraudAccusation.setSuspectName(suspectName);
		fraudAccusation.setPositionByAccusatorPositionId((Position)hibernateSession.load(Position.class, selectedAccusatorPositionId));
		fraudAccusation.setPositionBySuspectPositionId((Position)hibernateSession.load(Position.class, selectedSuspectPositionId));
		fraudAccusation.setIsMapped(false);
		hibernateSession.save(fraudAccusation);
		
		System.out.println(fraudAccusation.getFraudAccusationId());
		org.hibernate.Query query = hibernateSession.createSQLQuery("update fraud_accusation set fraud_accusation_number =concat('FM',fraud_accusation_id) where  fraud_accusation_id = " +fraudAccusation.getFraudAccusationId());
		query.executeUpdate();
		HibernateUtil.closeAll(hibernateSession, trx);
		
		return SUCCESS;
	}
	
	public String loadFraudInboxAction(){
		org.hibernate.Session hibernateSession= HibernateUtil.getSession();
		org.hibernate.Transaction trx = hibernateSession.beginTransaction();
		org.hibernate.Query query = hibernateSession.createQuery("from "+FraudAccusation.class.getName()+" where isMapped = false order by fraudAccusationId asc");
		query.setMaxResults(2000);
		List<FraudAccusation> fraudAccusations = query.list();
		
		for(FraudAccusation f:fraudAccusations){
			InboxView inboxView = new InboxView(); 
			inboxView.setAccusatorBranch(f.getBranchByAccusatorBranchId().getBranchName());
			inboxView.setSuspectBranch(f.getBranchBySuspectBranchId().getBranchName());
			inboxView.setCreatedDate(f.getCreatedDate());
			inboxView.setFraudAccusationId(f.getFraudAccusationId());
			inboxView.setFraudAccusationNumber(f.getFraudAccusationNumber());
			inboxViews.add(inboxView);
		}
		
		HibernateUtil.closeAll(hibernateSession, trx);
		return SUCCESS;
	}

	public String loadMappedFraudInboxAction(){
		org.hibernate.Session hibernateSession= HibernateUtil.getSession();
		org.hibernate.Transaction trx = hibernateSession.beginTransaction();
		org.hibernate.Query query = hibernateSession.createQuery("from "+FraudAccusation.class.getName()+" where isMapped = true order by fraudAccusationId asc");
		query.setMaxResults(2000);
		List<FraudAccusation> fraudAccusations = query.list();
		
		for(FraudAccusation f:fraudAccusations){
			InboxView inboxView = new InboxView(); 
			inboxView.setAccusatorBranch(f.getBranchByAccusatorBranchId().getBranchName());
			inboxView.setSuspectBranch(f.getBranchBySuspectBranchId().getBranchName());
			inboxView.setCreatedDate(f.getCreatedDate());
			inboxView.setFraudAccusationId(f.getFraudAccusationId());
			inboxView.setFraudAccusationNumber(f.getFraudAccusationNumber());
			inboxViews.add(inboxView);
		}
		
		HibernateUtil.closeAll(hibernateSession, trx);
		return SUCCESS;
	}
	
	public String loadFraudMappingAction(){
		org.hibernate.Session hibernateSession= HibernateUtil.getSession();
		org.hibernate.Transaction trx = hibernateSession.beginTransaction();
		org.hibernate.Query query = hibernateSession.createQuery("from "+Priority.class.getName());
		priorities = query.list();
		query = hibernateSession.createQuery("from "+FraudCategory.class.getName());
		fraudCategories = query.list();
		query = hibernateSession.createQuery("from "+User.class.getName());
		users = query.list();
		setFraudAccusation((FraudAccusation)hibernateSession.load(FraudAccusation.class, selectedFraudId));
		branchName = getFraudAccusation().getBranchByAccusatorBranchId().getBranchName();
		today = new Date();
		HibernateUtil.closeAll(hibernateSession, trx);
		return SUCCESS;
	}
	
	public String mappingNewFraudAction(){
		System.out.println(fraudAccusation.getFraudAccusationNumber());
		System.out.println(selectedPriorityId+ " "+ selectedFraudCategoryId+" "+selectedUserId);
		org.hibernate.Session hibernateSession= HibernateUtil.getSession();
		org.hibernate.Transaction trx = hibernateSession.beginTransaction();
		Query query = hibernateSession.createQuery("from "+ FraudAccusation.class.getName() + " where fraudAccusationNumber = '"+fraudAccusation.getFraudAccusationNumber()+"'");
		FraudAccusation fraudAccusation = (FraudAccusation) query.uniqueResult();
		
		FraudMapping fraudMapping = new FraudMapping();
		
		
		fraudMapping.setFraudCategory((FraudCategory) hibernateSession.load(FraudCategory.class, selectedFraudCategoryId));
		fraudMapping.setPriority((Priority) hibernateSession.load(Priority.class, selectedPriorityId));
		fraudMapping.setUser((User) hibernateSession.load(User.class, selectedUserId));
		fraudMapping.setFraudAccusation(fraudAccusation);
		
		hibernateSession.save(fraudMapping);
		fraudAccusation.setIsMapped(true);
		hibernateSession.saveOrUpdate(fraudAccusation);
		
		HibernateUtil.closeAll(hibernateSession, trx);
		return SUCCESS;
	}
	
	public String loadViewMappedInboxAction(){
		org.hibernate.Session hibernateSession= HibernateUtil.getSession();
		org.hibernate.Transaction trx = hibernateSession.beginTransaction();
		
		setFraudAccusation((FraudAccusation)hibernateSession.load(FraudAccusation.class, selectedMappedFraudId));
		org.hibernate.Query query = hibernateSession.createQuery("from "+FraudMapping.class.getName()+" where fraudAccusation="+getFraudAccusation().getFraudAccusationId());
		FraudMapping fraudMapping = (FraudMapping)query.uniqueResult();
		branchName = getFraudAccusation().getBranchByAccusatorBranchId().getBranchName();
		priority = fraudMapping.getPriority().getPriorityName();
		fraudCategory = fraudMapping.getFraudCategory().getFraudCategoryName();
		investigator = fraudMapping.getUser().getUsername();
		
		HibernateUtil.closeAll(hibernateSession, trx);
		return SUCCESS;
	}
	public Map<String, Object> getSessionAttributes() {
		return sessionAttributes;
	}

	public void setSessionAttributes(Map<String, Object> sessionAttributes) {
		this.sessionAttributes = sessionAttributes;
	}

	public String getAccusatorName() {
		return accusatorName;
	}

	public void setAccusatorName(String accusatorName) {
		this.accusatorName = accusatorName;
	}

	public String getSuspectName() {
		return suspectName;
	}

	public void setSuspectName(String suspectName) {
		this.suspectName = suspectName;
	}

	public Position getAccusatorPosition() {
		return accusatorPosition;
	}

	public void setAccusatorPosition(Position accusatorPosition) {
		this.accusatorPosition = accusatorPosition;
	}

	public Position getSuspectPosition() {
		return suspectPosition;
	}

	public void setSuspectPosition(Position suspectPosition) {
		this.suspectPosition = suspectPosition;
	}

	public Branch getAccustorBranch() {
		return accustorBranch;
	}

	public void setAccustorBranch(Branch accustorBranch) {
		this.accustorBranch = accustorBranch;
	}

	public Branch getSuspectBranch() {
		return suspectBranch;
	}

	public void setSuspectBranch(Branch suspectBranch) {
		this.suspectBranch = suspectBranch;
	}

	public String getAccusationDescription() {
		return accusationDescription;
	}

	public void setAccusationDescription(String accusationDescription) {
		this.accusationDescription = accusationDescription;
	}

	public Media getAccusationMedia() {
		return accusationMedia;
	}

	public void setAccusationMedia(Media accusationMedia) {
		this.accusationMedia = accusationMedia;
	}

	public List<Position> getPositions() {
		return positions;
	}

	public void setPositions(List<Position> positions) {
		this.positions = positions;
	}

	public List<Media> getMedias() {
		return medias;
	}

	public void setMedias(List<Media> medias) {
		this.medias = medias;
	}

	public List<Branch> getBranches() {
		return branches;
	}

	public void setBranches(List<Branch> branches) {
		this.branches = branches;
	}

	public Date getToday() {
		return today;
	}

	public void setToday(Date today) {
		this.today = today;
	}

	public Integer getSelectedAccusatorBranchId() {
		return selectedAccusatorBranchId;
	}

	public void setSelectedAccusatorBranchId(Integer selectedAccusatorBranchId) {
		this.selectedAccusatorBranchId = selectedAccusatorBranchId;
	}

	public Integer getSelectedAccusatorMediaId() {
		return selectedAccusatorMediaId;
	}

	public void setSelectedAccusatorMediaId(Integer selectedAccusatorMediaId) {
		this.selectedAccusatorMediaId = selectedAccusatorMediaId;
	}

	public Integer getSelectedAccusatorPositionId() {
		return selectedAccusatorPositionId;
	}

	public void setSelectedAccusatorPositionId(Integer selectedAccusatorPositionId) {
		this.selectedAccusatorPositionId = selectedAccusatorPositionId;
	}

	public Integer getSelectedSuspectPositionId() {
		return selectedSuspectPositionId;
	}

	public void setSelectedSuspectPositionId(Integer selectedSuspectPositionId) {
		this.selectedSuspectPositionId = selectedSuspectPositionId;
	}

	public Integer getSelectedSuspectBranchId() {
		return selectedSuspectBranchId;
	}

	public void setSelectedSuspectBranchId(Integer selectedSuspectBranchId) {
		this.selectedSuspectBranchId = selectedSuspectBranchId;
	}
	public List<InboxView> getInboxViews() {
		return inboxViews;
	}
	public void setInboxViews(List<InboxView> inboxViews) {
		this.inboxViews = inboxViews;
	}
	public List<Priority> getPriorities() {
		return priorities;
	}
	public void setPriorities(List<Priority> priorities) {
		this.priorities = priorities;
	}
	public List<FraudCategory> getFraudCategories() {
		return fraudCategories;
	}
	public void setFraudCategories(List<FraudCategory> fraudCategories) {
		this.fraudCategories = fraudCategories;
	}
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public Integer getSelectedFraudId() {
		return selectedFraudId;
	}
	public void setSelectedFraudId(Integer selectedFraudId) {
		this.selectedFraudId = selectedFraudId;
	}
	public FraudAccusation getFraudAccusation() {
		return fraudAccusation;
	}
	public void setFraudAccusation(FraudAccusation fraudAccusation) {
		this.fraudAccusation = fraudAccusation;
	}
	public String getBranchName() {
		return branchName;
	}
	public void setBranchName(String branchName) {
		this.branchName = branchName;
	}
	public Integer getSelectedUserId() {
		return selectedUserId;
	}
	public void setSelectedUserId(Integer selectedUserId) {
		this.selectedUserId = selectedUserId;
	}
	public Integer getSelectedFraudCategoryId() {
		return selectedFraudCategoryId;
	}
	public void setSelectedFraudCategoryId(Integer selectedFraudCategoryId) {
		this.selectedFraudCategoryId = selectedFraudCategoryId;
	}
	public Integer getSelectedPriorityId() {
		return selectedPriorityId;
	}
	public void setSelectedPriorityId(Integer selectedPriorityId) {
		this.selectedPriorityId = selectedPriorityId;
	}
	public Integer getSelectedMappedFraudId() {
		return selectedMappedFraudId;
	}
	public void setSelectedMappedFraudId(Integer selectedMappedFraudId) {
		this.selectedMappedFraudId = selectedMappedFraudId;
	}
	public String getPriority() {
		return priority;
	}
	public void setPriority(String priority) {
		this.priority = priority;
	}
	public String getFraudCategory() {
		return fraudCategory;
	}
	public void setFraudCategory(String fraudCategory) {
		this.fraudCategory = fraudCategory;
	}
	public String getInvestigator() {
		return investigator;
	}
	public void setInvestigator(String investigator) {
		this.investigator = investigator;
	}
	
	
	
}
